//
//  myTimer.m
//  One
//
//  Created by mac on 16/5/28.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "myTimer.h"

@implementation myTimer

- (BOOL)judgeIsFireTimerWithDateObj:(MovieListObject *)obj cellIndexRow:(int)row
{
    [self getCurrentDate];
    NSArray *timeArr = [obj.releasetime componentsSeparatedByString:@" "];
    
    if ([nowArr[0] isEqualToString:[NSString stringWithFormat:@"%@",timeArr[0]]]) {
        
        NSString *timeStr = [nowArr lastObject];
        NSArray *TimeNumArr = [timeStr componentsSeparatedByString:@":"];
        int hc = 23 - [TimeNumArr[0] intValue];
        int mc = 60 - [TimeNumArr[1] intValue];
        int sc = 60 - [TimeNumArr[2] intValue];
        int totalCount = hc * 60 * 60 + mc* 60 + sc;
        totalTimeArray = [[NSMutableArray alloc] init];
        NSDictionary *dic = @{
                              @"indexPath":[NSString stringWithFormat:@"%d",row],
                              @"totalTime":[NSNumber numberWithInt:totalCount]
                              };
        [totalTimeArray addObject:dic];
        [self fireTheNStimer];
        return YES;
    }else
    {
        return NO;
    }
}

- (void)getCurrentDate
{
    NSDateFormatter *formate = [[NSDateFormatter alloc ] init];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *now = [NSDate  date];
    NSString *nowTime = [formate stringFromDate:now];
    nowArr = [nowTime componentsSeparatedByString:@" "];
}

- (void)fireTheNStimer
{
    if (timer == nil) {
        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
        [timer fire];
    }else
    {
        
        return;
    }
}

- (void)onTimer
{
    int time;
    for (int i = 0; i <totalTimeArray.count; i ++) {
        time = [[[totalTimeArray objectAtIndex:i] objectForKey:@"totalTime"] intValue];
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[[[totalTimeArray objectAtIndex:i] objectForKey:@"indexPath"] intValue] inSection:0];
        NSDictionary *dic = @{
                              @"indexPath":[NSString stringWithFormat:@"%ld",(long)indexPath.row],
                              @"totalTime":[NSString stringWithFormat:@"%d",--time]
                              };
        [totalTimeArray replaceObjectAtIndex:i withObject:dic];
        
       
    }
    
}

- (NSString *)lessSecondWithSecond:(int)totalTime
{
    int hour =  totalTime/3600;
    int min = (totalTime%3600)/60;
    int sec = totalTime%60;
    
    NSString *str1 = [self judgeWithTime:hour];
    NSString *str2 = [self judgeWithTime:min];
    NSString *str3 = [self judgeWithTime:sec];
    NSString *time = [NSString stringWithFormat:@"%@:%@:%@",str1,str2,str3];
    return time;
}

- (NSString *)judgeWithTime:(int)num
{
    NSString *str;
    if (num < 10) {
        str = [NSString stringWithFormat:@"0%d",num];
    }else
    {
        str = [NSString stringWithFormat:@"%d",num];
    }
    return str;
}


@end
